package config

import (
	"fmt"
	"github.com/spf13/viper"
)

const (
	dbSQLDriver        = "mysql"
	dbMaxIdle          = 10
	dbMaxConn          = 10
	dbMaxLifeTime      = 0
	dbDefaultRowsLimit = 0
	dbServiceKey       = ""
	dbName             = "nftdatabase"
	dbRunMode          = "vip"

	configPath1 = "/data01/NFT-Server"
	configPath2 = "/opt/NFT-Server"
	configPath3 = "./conf"
)

// NftConfig global config
type NftConfig struct {
	CommonConfig      CommonConfig
	DBConfig          DBConfig
	DACConfig         DACConfig
	OBSConfig         OBSConfig
	IVSConfig         IVSConfig
	AppConfig         AppConfig
	SavePictureConfig SavePictureConfig
	SmsMsgConfig      SmsMsgConfig
}

var conf *NftConfig

type CommonConfig struct {
	IsHisDeploy bool
}

// DBConfig define the config of connect database
type DBConfig struct {
	SQLDriver        string `json:"sql_driver"`
	MaxIdle          int    `json:"max_idle"`
	MaxConn          int    `json:"max_conn"`
	MaxLifeTime      int    `json:"max_life_time"`
	DefaultRowsLimit int    `json:"default_rows_limit"`
	DBIP             string `json:"db_ip"`
	ServiceKey       string `json:"service_key"`
	DBPort           string `json:"db_port"`
	DBName           string `json:"db_name"`
	DBUser           string `json:"db_user"`
	DBPassword       string `json:"db_password"`
	DBRunMode        string `json:"db_run_mode"`
}

// DACConfig define the config of DAC
type DACConfig struct {
	ConfigPath string
}

type OBSConfig struct {
	Endpoint   string
	AK         string
	SK         string
	BucketName string
}
type IVSConfig struct {
	Endpoint string
	AK       string
	SK       string
}

// AdminConfig  define the name and password of the admin
type AdminConfig struct {
	Name     string
	Password string
}

type AppConfig struct {
	AlphaMode bool
}

type HisConfig struct {
	StaticToken   string
	MainSysConfig string
	SubSysConfig  string
}

//add:save picture on disk
type SavePictureConfig struct {
	SavePath string
}

type SmsMsgConfig struct {
	ApiAddress string
	APP_Key    string
	APP_Secret string
	ChannelId  string
	TemplateId string
	Signature  string
}

func init() {
	if err := SetConfig(); err != nil {
		fmt.Printf("Failed to init nft server conifg: %v", err)
	}
}

// SetConfig set global config by env
func SetConfig() error {
	config := viper.New()

	// for windows
	config.AddConfigPath(configPath1)
	config.AddConfigPath(configPath2)
	config.AddConfigPath(configPath3)
	config.SetConfigName("config")
	config.SetConfigType("ini")
	if err := config.ReadInConfig(); err != nil {
		return err
	}

	conf = &NftConfig{
		CommonConfig{IsHisDeploy: config.GetBool("CommonConfig.IsHisDeploy")},
		DBConfig{
			dbSQLDriver,
			dbMaxIdle,
			dbMaxConn,
			dbMaxLifeTime,
			dbDefaultRowsLimit,
			config.GetString("DBConfig.DB_VIP"),
			dbServiceKey,
			config.GetString("DBConfig.DB_PORT"),
			dbName,
			config.GetString("DBConfig.DB_USER"),
			config.GetString("DBConfig.DB_PWD"),
			dbRunMode,
		}, DACConfig{
			ConfigPath: config.GetString("DACConfig.DAC_CONFIG_PATH"),
		}, OBSConfig{
			Endpoint:   config.GetString("OBSConfig.OBS_ENDPOINT"),
			AK:         config.GetString("OBSConfig.OBS_AK"),
			SK:         config.GetString("OBSConfig.OBS_SK"),
			BucketName: config.GetString("OBSConfig.OBS_BUCKETNAME"),
		}, IVSConfig{
			Endpoint: config.GetString("IVSConfig.IVS_ENDPOINT"),
			AK:       config.GetString("IVSConfig.IVS_AK"),
			SK:       config.GetString("IVSConfig.IVS_SK"),
		}, AppConfig{
			AlphaMode: config.GetBool("AppConfig.AlphaMode"),
		}, SavePictureConfig{
			SavePath: config.GetString("SavePicturePath.SavePath"),
		}, SmsMsgConfig{
			ApiAddress: config.GetString("SmsMsgConfig.ApiAddress"),
			APP_Key:    config.GetString("SmsMsgConfig.APP_Key"),
			APP_Secret: config.GetString("SmsMsgConfig.APP_Secret"),
			ChannelId:  config.GetString("SmsMsgConfig.ChannelId"),
			TemplateId: config.GetString("SmsMsgConfig.TemplateId"),
			Signature:  config.GetString("SmsMsgConfig.Signature"),
		},
	}
	return nil
}

func GetConfig() *NftConfig {
	return conf
}
